﻿Public Class frmMain

    Private Sub ItemsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.ItemsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.InventoryDataSet)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'InventoryDataSet.items' table. You can move, or remove it, as needed.
        Me.ItemsTableAdapter.Fill(Me.InventoryDataSet.items)

        ItemPurchaseDateDateTimePicker.Enabled = False


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'All the textboxes are readonly upon initial program loading and throughout the program.
        'This is to prevent accidental editing.
        ItemNameTextBox.ReadOnly = False
        ItemModelTextBox.ReadOnly = False
        ItemSerialNumberTextBox.ReadOnly = False
        ItemWarrantyTextBox.ReadOnly = False
        ItemCostTextBox.ReadOnly = False
        ItemValueTextBox.ReadOnly = False
        ItemQuantityTextBox.ReadOnly = False
        ItemManufacturerTextBox.ReadOnly = False
        ItemCategoryTextBox.ReadOnly = False
        ItemLocationTextBox.ReadOnly = False
        ItemConditionTextBox.ReadOnly = False
        ItemInsuredTextBox.ReadOnly = False
        ItemDescriptionTextBox.ReadOnly = False
        ItemPurchaseDateDateTimePicker.Enabled = True
        ListBox1.Enabled = False
        btnSave.Visible = True
        btnDeleteItem.Visible = False
        btnExit.Visible = False
        btnReports.Visible = False
        btnAddItem.Visible = False
        btnCancelEdit.Visible = True
        btnEdit.Visible = False
        btnEditImage.Visible = True

    End Sub

    Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
        'Hide frmMain show frmAdd
        frmAdd.Show()
        Me.Hide()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()

    End Sub

    Private Sub btnDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteItem.Click
        'verify if user wants to delete the current item
        If MsgBox("Are you sure you want to delete this record?", _
                  vbQuestion + vbYesNo + vbDefaultButton2, _
                  "Enigma Home Inventory") = vbNo Then

            Exit Sub

        End If

        Me.Validate()
        Me.ItemsBindingSource.RemoveCurrent()
        Me.TableAdapterManager.UpdateAll(Me.InventoryDataSet)

    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start("http://enhi.codeplex.com/discussions/")

    End Sub

    Private Sub btnReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReports.Click
        frmReport.Show()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'Verify the user wants to save the changes they have made to the current record.
        If MsgBox("Are you sure you want to update this record?", _
          vbQuestion + vbYesNo + vbDefaultButton2, _
          "Enigma Home Inventory") = vbNo Then

            ItemNameTextBox.ReadOnly = True
            ItemModelTextBox.ReadOnly = True
            ItemSerialNumberTextBox.ReadOnly = True
            ItemWarrantyTextBox.ReadOnly = True
            ItemCostTextBox.ReadOnly = True
            ItemValueTextBox.ReadOnly = True
            ItemQuantityTextBox.ReadOnly = True
            ItemManufacturerTextBox.ReadOnly = True
            ItemCategoryTextBox.ReadOnly = True
            ItemLocationTextBox.ReadOnly = True
            ItemConditionTextBox.ReadOnly = True
            ItemInsuredTextBox.ReadOnly = True
            ItemDescriptionTextBox.ReadOnly = True
            ItemPurchaseDateDateTimePicker.Enabled = False
            ListBox1.Enabled = True
            btnSave.Visible = False
            btnDeleteItem.Visible = True
            btnExit.Visible = True
            btnReports.Visible = True
            btnAddItem.Visible = True
            btnCancelEdit.Visible = False
            btnEdit.Visible = True
            btnEditImage.Visible = False

            Exit Sub

        End If

        Me.Validate()
        Me.ItemsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.InventoryDataSet)

        'Make all textboxes back to read only to prevent accidental editing.
        ItemNameTextBox.ReadOnly = True
        ItemModelTextBox.ReadOnly = True
        ItemSerialNumberTextBox.ReadOnly = True
        ItemWarrantyTextBox.ReadOnly = True
        ItemCostTextBox.ReadOnly = True
        ItemValueTextBox.ReadOnly = True
        ItemQuantityTextBox.ReadOnly = True
        ItemManufacturerTextBox.ReadOnly = True
        ItemCategoryTextBox.ReadOnly = True
        ItemLocationTextBox.ReadOnly = True
        ItemConditionTextBox.ReadOnly = True
        ItemInsuredTextBox.ReadOnly = True
        ItemDescriptionTextBox.ReadOnly = True
        ItemPurchaseDateDateTimePicker.Enabled = False
        ListBox1.Enabled = True
        btnSave.Visible = False
        btnDeleteItem.Visible = True
        btnExit.Visible = True
        btnReports.Visible = True
        btnAddItem.Visible = True
        btnCancelEdit.Visible = False
        btnEdit.Visible = True
        btnEditImage.Visible = False

    End Sub

    Private Sub btnCancelEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelEdit.Click
        'If the user cancels the edit then we want to make sure the changes they made
        'are not saved. We also want to undo any changes they made to prevent
        'the textboxes from showing the stuff that user typed in. This is pretty
        'crude way of doing it, but until another way is found this will have to do
        If MsgBox("Are you sure you want to cancel editing this record?", _
  vbQuestion + vbYesNo + vbDefaultButton2, _
  "Enigma Home Inventory") = vbNo Then

            Exit Sub

        End If

        ItemNameTextBox.ReadOnly = True
        ItemModelTextBox.ReadOnly = True
        ItemSerialNumberTextBox.ReadOnly = True
        ItemWarrantyTextBox.ReadOnly = True
        ItemCostTextBox.ReadOnly = True
        ItemValueTextBox.ReadOnly = True
        ItemQuantityTextBox.ReadOnly = True
        ItemManufacturerTextBox.ReadOnly = True
        ItemCategoryTextBox.ReadOnly = True
        ItemLocationTextBox.ReadOnly = True
        ItemConditionTextBox.ReadOnly = True
        ItemInsuredTextBox.ReadOnly = True
        ItemDescriptionTextBox.ReadOnly = True
        ItemPurchaseDateDateTimePicker.Enabled = False
        ListBox1.Enabled = True
        btnSave.Visible = False
        btnDeleteItem.Visible = True
        btnExit.Visible = True
        btnReports.Visible = True
        btnAddItem.Visible = True
        btnCancelEdit.Visible = False
        btnEdit.Visible = True
        btnEditImage.Visible = False

        Me.Validate()
        Me.ItemsBindingSource.CancelEdit()
        Me.TableAdapterManager.UpdateAll(Me.InventoryDataSet)



    End Sub

    Private Sub btnEditImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditImage.Click
        Me.OpenFileDialog1.FileName = Nothing

        Me.OpenFileDialog1.ShowDialog()

        If Not Me.OpenFileDialog1.FileName = Nothing Then
            Me.ItemImagePictureBox.ImageLocation = Me.OpenFileDialog1.FileName
        End If
    End Sub

#Region " Windows Form Designer generated code "

    'disables close
    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            Const CS_NOCLOSE As Integer = &H200
            cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
            Return cp
        End Get
    End Property
#End Region

End Class